package com.bdqn.dao.impl;

import com.bdqn.dao.BaseDao;
import com.bdqn.dao.BrandDao;
import com.bdqn.entity.TbBrand;
import com.bdqn.entity.TbBrand;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BrandDaoImpl extends BaseDao implements BrandDao {
    @Override
    public List<TbBrand> selectAll() {
        ArrayList<TbBrand> brands = new ArrayList<>();
        //获取数据库连接对象
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            //构造PreparedStatement对象
            pstmt = conn.prepareStatement("select * from tb_brand");
            //执行sql语句
            rs = pstmt.executeQuery();
            //处理返回结果
            while (rs.next()) {
                TbBrand brand = new TbBrand();
                brand.setId(rs.getInt("id"));
                brand.setBrandName(rs.getString("brand_name"));
                brand.setCompanyName(rs.getString("company_name"));
                brand.setOrdered(rs.getInt("ordered"));
                brand.setDescription(rs.getString("description"));
                brand.setStatus(rs.getInt("status"));
                brands.add(brand);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, pstmt, rs);
        }
        return brands;
    }

    @Override
    public int add(TbBrand brand) {
        //构造sql语句
        String sql = "insert into tb_brand values(null,?,?,?,?,?)";
        //封装参数信息
        ArrayList<Object> params = new ArrayList<>();
        params.add(brand.getBrandName());
        params.add(brand.getCompanyName());
        params.add(brand.getOrdered());
        params.add(brand.getDescription());
        params.add(brand.getStatus());
        return exceuteUpdate(sql, params.toArray());
    }

    @Override
    public TbBrand selectById(int id) {
        TbBrand brand = new TbBrand();
        //获取数据库连接对象
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            //构造PreparedStatement对象
            pstmt = conn.prepareStatement("select * from tb_brand where id = ?");
            //给？赋值
            pstmt.setInt(1, id);
            //执行sql语句
            rs = pstmt.executeQuery();
            //处理返回结果
            while (rs.next()) {
                brand.setId(rs.getInt("id"));
                brand.setBrandName(rs.getString("brand_name"));
                brand.setCompanyName(rs.getString("company_name"));
                brand.setOrdered(rs.getInt("ordered"));
                brand.setDescription(rs.getString("description"));
                brand.setStatus(rs.getInt("status"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, pstmt, rs);
        }
        return brand;
    }

    @Override
    public int update(TbBrand brand) {
        //构造sql语句
        String sql = "update tb_brand set brand_name = ?,company_name = ?,ordered = ?,description = ?,status = ? where id = ?";
        //封装参数信息
        ArrayList<Object> params = new ArrayList<>();
        params.add(brand.getBrandName());
        params.add(brand.getCompanyName());
        params.add(brand.getOrdered());
        params.add(brand.getDescription());
        params.add(brand.getStatus());
        params.add(brand.getId());
        return exceuteUpdate(sql, params.toArray());
    }

    @Override
    public boolean delete(String id) {
        //获取数据库连接对象
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        try {
            // 定义 SQL 语句
            String sql = "DELETE FROM tb_brand WHERE id = ?";
            //构造PreparedStatement对象
            pstmt = conn.prepareStatement(sql);
            // 设置参数
            pstmt.setString(1, id);
            //执行sql语句
            int rowsAffected = pstmt.executeUpdate();
            //处理返回结果
            // 如果受影响的行数大于 0，则删除成功
            if (rowsAffected > 0) {
                return true;
            }
            //关闭资源
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
